package com.ruoyi.web.controller.process;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.domain.process.ProcessRibbon;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.processService.IProcessInventoryService;
import com.ruoyi.system.service.processService.IProcessRibbonService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 并卷Controller
 * 
 * @author ljx
 * @date 2020-07-07
 */
@Controller
@RequestMapping("/process/ribbon")
public class ProcessRibbonController extends BaseController
{
    private String prefix = "process/ribbon";

    @Autowired
    private IProcessRibbonService processRibbonService;
    @Resource
    private IProcessInventoryService iProcessInventoryService;
    @Resource
    private ISysUserService iSysUserService;

    @RequiresPermissions("process:ribbon:view")
    @GetMapping()
    public String ribbon()
    {
        return prefix + "/ribbon";
    }

    /**
     * 查询并卷列表
     */
    @RequiresPermissions("process:ribbon:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessRibbon processRibbon)
    {
        startPage();
        List<ProcessRibbon> list = processRibbonService.selectProcessRibbonList(processRibbon);
        return getDataTable(list);
    }

    /**
     * 导出并卷列表
     */
    @RequiresPermissions("process:ribbon:export")
    @Log(title = "并卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessRibbon processRibbon)
    {
        List<ProcessRibbon> list = processRibbonService.selectProcessRibbonList(processRibbon);
        list.stream().forEach(ribbon -> {
            ribbon.setSurfaceName(ribbon.getSurface()==1?"内":"外");
        });
        ExcelUtil<ProcessRibbon> util = new ExcelUtil<ProcessRibbon>(ProcessRibbon.class);
        return util.exportExcel(list, "ribbon");
    }

    /**
     * 新增并卷
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        ProcessInventory inventory = new ProcessInventory();
        inventory.setStatus(1);
        inventory.setType(Long.valueOf(2));
        List<ProcessInventory> list = iProcessInventoryService.selectProcessInventoryList(inventory);
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("users",users);
        mmap.put("inventorys",list);
        return prefix + "/add";
    }

    /**
     * 新增保存并卷
     */
    @RequiresPermissions("process:ribbon:add")
    @Log(title = "并卷", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessRibbon processRibbon)
    {
        processRibbon.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(processRibbonService.insertProcessRibbon(processRibbon));
    }

    /**
     * 修改并卷
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //获得并卷已绑定的原膜编号
        List<ProcessInventory> list =processRibbonService.getProcessInventory(id);
        //查询并卷绑定的原膜是否有被升级加工过
        List<ProcessInventory> collect = list.stream().filter(p -> p.getStatus() > 2).collect(Collectors.toList());
        //包含并卷基本信息+原膜拼接信息
        ProcessRibbon processRibbon = processRibbonService.selectProcessRibbonById(id);
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("users",users);
        mmap.put("inventorys",list);
        mmap.put("processRibbon", processRibbon);
        //有被升级加工过的不可修改，无则可修改
        if(CollectionUtils.isNotEmpty(collect)){
            return prefix + "/edit";
        }
        ProcessInventory inventory = new ProcessInventory();
        inventory.setStatus(1);
        inventory.setType(Long.valueOf(2));
        List<ProcessInventory> noList = iProcessInventoryService.selectProcessInventoryList(inventory);
        list.addAll(noList);
        mmap.put("inventorys",list);
        return prefix + "/canEdit";
    }

    /**
     * 修改保存并卷
     */
    @RequiresPermissions("process:ribbon:edit")
    @Log(title = "并卷", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessRibbon processRibbon)
    {
        processRibbon.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(processRibbonService.updateProcessRibbon(processRibbon));
    }

    /**
     * 删除并卷
     */
    @RequiresPermissions("process:ribbon:remove")
    @Log(title = "并卷", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return processRibbonService.deleteProcessRibbonByIds(ids);
    }
}
